package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.CourseSchedulingDetail;
import com.xmy.cultivate.mapper.CourseSchedulingDetailMapper;
import com.xmy.cultivate.service.ICourseSchedulingDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 排课详情 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class CourseSchedulingDetailServiceImpl extends ServiceImpl<CourseSchedulingDetailMapper, CourseSchedulingDetail> implements ICourseSchedulingDetailService {

    @Autowired
    @Lazy
    CourseSchedulingDetailMapper courseSchedulingDetailMapper;

    @Override
    public Integer getSchedulingCountForRecord(Long gradeId){
        return courseSchedulingDetailMapper.getSchedulingCountForRecord(gradeId);
    }

    /**
     * 删除当前时间之后的排课
     * @param gradeId
     * @return
     */
    @Override
    public boolean deleteForGradeId(Long gradeId, LocalDate localDate){
        Long rs = courseSchedulingDetailMapper.deleteForGradeId(gradeId,localDate);
        if (rs != null && rs>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Long getIsAtForDate(Long gradeId, LocalDate schoolDate){
        return  courseSchedulingDetailMapper.getIsAtForDate(gradeId,schoolDate);
    }

    @Override
    public Integer getNextLessonNum(Long gradeId,LocalDate schoolDate){
        return courseSchedulingDetailMapper.getNextLessonNum(gradeId,schoolDate);
    }

    @Override
    public CourseSchedulingDetail getOneForId(Long Id){
        return courseSchedulingDetailMapper.getOneForId(Id);
    }

    @Override
    public CourseSchedulingDetail getOneForOutId(Long Id){
        return courseSchedulingDetailMapper.getOneForOutId(Id);
    }

    @Override
    public Integer updateDeleted(Long id, Integer deleted){
        return courseSchedulingDetailMapper.updateDeleted(id,deleted);
    }

    @Override
    public List<CourseSchedulingDetail> getSchedulingClash(Integer yearPart, Integer quarterNum, Long studentId, Long gradeId, String localDateTime){
        List<CourseSchedulingDetail> repairCourseList = this.getSchedulingList(yearPart,quarterNum,studentId,1,localDateTime);
        List<CourseSchedulingDetail> courseSchedulingDetailList = courseSchedulingDetailMapper.getSchedulingForGradeId(yearPart,quarterNum,gradeId,1,localDateTime);

        //冲突排课
        List<CourseSchedulingDetail> repairCourseClashList = new ArrayList<>();
        for (CourseSchedulingDetail courseSchedulingDetail:courseSchedulingDetailList){
            for (CourseSchedulingDetail repairCourse:repairCourseList){
                if ((courseSchedulingDetail.getStartDateTime().compareTo(repairCourse.getStartDateTime())<=0 && courseSchedulingDetail.getEndDateTime().compareTo(repairCourse.getStartDateTime())>=0) || (courseSchedulingDetail.getStartDateTime().compareTo(repairCourse.getEndDateTime())<=0 && courseSchedulingDetail.getEndDateTime().compareTo(repairCourse.getEndDateTime())>=0) || (courseSchedulingDetail.getStartDateTime().compareTo(repairCourse.getEndDateTime())>=0 && courseSchedulingDetail.getEndDateTime().compareTo(repairCourse.getEndDateTime())<=0)){
                    repairCourseClashList.add(repairCourse);
                }
            }
        }
        return repairCourseClashList;
    }

    public List<CourseSchedulingDetail> getSchedulingList(Integer yearPart, Integer quarterNum, Long studentId, Integer lessonType, String localDateTime){
        return courseSchedulingDetailMapper.getSchedulingList(yearPart,quarterNum,studentId,lessonType,localDateTime);
    }
}
